﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using LEDDataBase;
namespace LEDManager
{
    /// <summary>
    /// Interaction logic for LED.xaml
    /// </summary>
    public partial class LEDManagement : UserControl
    {
        public LEDData m_led;
        public bool IsAnyChange = false;
        public string LLLEDID = "01";
       decimal xPoint
        {
            get
            {
                decimal x = 0;
                if(dwMASTER!=null)
                {
                    x = System.Convert.ToDecimal(dwMASTER["LMLEDXPOINT"].ToString());
            }
                return x;
            }
        }
       decimal yPoint
       {
           get
           {
               decimal y = 0;
               if (dwMASTER != null)
               {
                   y = System.Convert.ToDecimal(dwMASTER["LMLEDYPOINT"].ToString());
               }
               return y;
           }
       }
        public LEDDataSet LocalData;
        LEDDataSet setUI
        {
            get
            {
                return (LEDDataSet)this.FindResource("lEDDataSet");
            }
        }
        CollectionViewSource vwLEDLIST
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLEDLIST");
            }
        }
        CollectionViewSource vwMASTER
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwMASTER");
            }
        }
        CollectionViewSource vwLEDLOCATION
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLEDLOCATION");
            }
        }
        CollectionViewSource vwPROGRAM
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwPROGRAM");
            }
        }
        CollectionViewSource vwLEAF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLEAF");
            }
        }
        DataRow dwLEAF
        {
            get
            {
                return ((DataRowView)vwLEAF.View.CurrentItem).Row;
            }

        }
        CollectionViewSource vwSPEEDDEF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwSPEEDDEF");
            }
        }
        CollectionViewSource vwCCOUNT
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwCCOUNT");
            }
        }
        CollectionViewSource vwCMOVIE
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwCMOVIE");
            }
        }
        CollectionViewSource vwCPICTURE
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwCPICTURE");
            }
        }
        CollectionViewSource vwCTEXT
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwCTEXT");
            }
        }
        CollectionViewSource vwEFFECTDEF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwEFFECTDEF");
            }
        }
        CollectionViewSource vwFORMATDEF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwFORMATDEF");
            }
        }
        CollectionViewSource vwFONTTABLE
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwFONTTABLE");
            }
        }
        CollectionViewSource vwCONTENTTYPE
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwCONTENTTYPE");
            }
        } 
        DataRow dwCONTENTTYPE
        {
            get
            {
                if (vwCONTENTTYPE.View.CurrentItem == null)
                    vwCONTENTTYPE.View.MoveCurrentToFirst();
                return ((DataRowView)vwCONTENTTYPE.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwLEAFDETAIL
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLEAFDETAIL");
            }
        }
        DataRow dwLEAFDETAIL
        {
            get
            {
                return ((DataRowView)vwLEAFDETAIL.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwLAYOUT
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLAYOUT");
            }
        }
        DataRow dwLAYOUT
        {
            get
            {
                return ((DataRowView)vwLAYOUT.View.CurrentItem).Row;
            }

        }
        CollectionViewSource vwLAYOUTMASTER
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLAYOUTMASTER");
            }
        }
        DataRow dwLAYOUTMASTER
        {
            get
            {
                return ((DataRowView)vwLAYOUTMASTER.View.CurrentItem).Row;
            }
        }
        DataRow dwMASTER
        {
            get
            {
                return ((DataRowView)vwMASTER.View.CurrentItem).Row;
            }

        }
        DataRow dwPROGRAM
        {
            get
            {
                return ((DataRowView)vwPROGRAM.View.CurrentItem).Row;
            }
        }
        DataRow dwLEDLIST
        {
            get
            {
                return ((DataRowView)vwLEDLIST.View.CurrentItem).Row;
            }
        }
        public LEDManagement()
        {
            InitializeComponent();
            m_led = new LEDData();
            LocalData = new LEDDataSet();
        }

        private void dlgLEDLIST_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataSet setResult = new DataSet();
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    //dwLEDLIST = drv.Row;
                }
            }
            catch (Exception ex)
            {
                //Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void cmdLoadSystem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI.Clear();
                LocalData.Clear();

                DataSet setResult=m_led.LoadLedData();
                foreach(DataTable tbTemp in setResult.Tables)
                {
                    LocalData.Merge(tbTemp);
                }
                setUI.Merge(LocalData.LEDLIST);
                setUI.Merge(LocalData.LAYOUT);
                setUI.Merge(LocalData.CONTENTTYPE);
                vwLEDLIST.View.MoveCurrentToFirst();
                cmdEditLED_Click(null, null);
                cmdLoadProg_Click(null, null);
                cmdLoadLeaf_Click(null, null);
            }
            catch (Exception ex)
            {
            }
        }

        private void cmdEditLED_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RefreshLED();
            }
            catch (Exception ex)
            {
            }
        }
     
        private void cmdSaveSystem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_led.SaveLocalData(LocalData);
            }
            catch (Exception ex)
            { 
            }

        }

        private void cmdSaveMaster_Click(object sender, RoutedEventArgs e)
        {
            MergeData(dwMASTER);
        }
        private void cmdNewProg_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PRPROGRAMID = "P"+m_led.Right("00"+dwMASTER["LMMAXPROGID"].ToString(),2);
                DataRow dwTemp= LocalData.PROGRAM.NewPROGRAMRow();
                dwTemp["PRSYSTEM"] = dwMASTER["LMSYSTEM"];
                dwTemp["PRLEDID"] = dwMASTER["LMLEDID"];
                dwTemp["PRPROGRAMID"] = PRPROGRAMID;
                dwMASTER["LMMAXPROGID"] = System.Convert.ToInt32(dwMASTER["LMMAXPROGID"].ToString()) + 1;
                MergeData(dwMASTER);
                MergeData(dwTemp);
                RefreshProg();
                while (true)
                {
                    if (dwPROGRAM == null)
                        break;
                    if (dwPROGRAM["PRPROGRAMID"].ToString() != PRPROGRAMID)
                    {
                        vwPROGRAM.View.MoveCurrentToNext();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        } 
        private void cmdLoadProg_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RefreshProg();
            }
            catch (Exception ex)
            {
            }
        }
       
        private void cmdSaveProg_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MergeData(dwPROGRAM); 
            }
            catch (Exception ex)
            {
            }
        }
        #region=====LocalData=============
        private void MergeData(DataRow dwIncoming)
        {
            try
            {
                string expression = "";
                string cname="";
                string tbname=dwIncoming.Table.TableName;
                foreach (DataColumn key in LocalData.Tables[tbname].PrimaryKey)
                { 
                    cname=key.ColumnName;
                    expression += cname + "='" + dwIncoming[cname].ToString() + "' and ";
                }
                expression += "***";
                expression = expression.Replace("and ***", "");
                DataRow[] rows = LocalData.Tables[tbname].Select(expression);
                if (rows.Length==0)
                {
                    LocalData.Tables[tbname].Rows.Add(dwIncoming);
                }
                foreach (DataRow dwRow in rows)
                {
                    dwRow.ItemArray = dwIncoming.ItemArray;
                }
            }
            catch (Exception ex)
            { 
            }
        }
        private void RefreshLED()
        {
            try
            {
                if (dwLEDLIST == null)
                    return;
                setUI.MASTER.Clear();
                LLLEDID = dwLEDLIST["LLLEDID"].ToString();
                string expression = "LMLEDID='" + LLLEDID + "'";
                DataRow[] rows = LocalData.MASTER.Select(expression);
                setUI.Merge(rows);
                vwMASTER.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
            }
        }
        private void RefreshProg()
        {
            try
            {
                string expression = "PRLEDID='" + LLLEDID + "'";
                DataRow[] rows = LocalData.PROGRAM.Select(expression);
                setUI.Merge(rows);
                vwPROGRAM.View.MoveCurrentToFirst();
                //setUI.Merge(LocalData.PROGRAM);
            }
            catch (Exception ex)
            {
            }
        }
        private void RefreshLeaf()
        {
            try
            {
                //"PRSYSTEM", "PRLEDID", "PRPROGRAMID"
                //"LFSYSTEM", "LFLEDID", "LFPROGRAMID", "LFLEAFID",
                if (dwPROGRAM == null)
                    return;
                string sys = dwPROGRAM["PRSYSTEM"].ToString();
                string led = dwPROGRAM["PRLEDID"].ToString();
                string pgid = dwPROGRAM["PRPROGRAMID"].ToString();
                string expression = "LFSYSTEM='" + sys + "' and LFLEDID='" + led + "' and LFPROGRAMID='" + pgid + "'";
                DataRow[] rows = LocalData.LEAF.Select(expression);
                setUI.LEAF.Clear();
                setUI.Merge(rows);
                vwLEAF.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void RefreshLeafDetail()
        {
            try
            {
                //"LFSYSTEM", "LFLEDID", "LFPROGRAMID"
                //"LDSYSTEM", "LDLEDID", "LDPROGRAMID", "LDLEAFID",LDAREAID,
                if (dwLEAF == null)
                    return;
                string sys = dwLEAF["LFSYSTEM"].ToString();
                string led = dwLEAF["LFLEDID"].ToString();
                string pgid = dwLEAF["LFPROGRAMID"].ToString();
                string lfid = dwLEAF["LFLEAFID"].ToString();
                string expression = "LDSYSTEM='" + sys + "' and LDLEDID='" + led + "' and LDPROGRAMID='" + pgid + "' and LDLEAFID='"+lfid+"'";
                DataRow[] rows = LocalData.LEAFDETAIL.Select(expression); 
                 setUI.LEAFDETAIL.Clear();
                setUI.AcceptChanges();
                setUI.LEAFDETAIL.AcceptChanges(); 
                setUI.Merge(rows);
                vwLEAFDETAIL.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void RefreshLayOutMaster()
        {
            try
            {
                //"LYSYSTEM", "LYLAYOUTID", "LYLAYOUTNAME"

                string sys = dwLAYOUT["LYSYSTEM"].ToString();
                string lay = dwLAYOUT["LYLAYOUTID"].ToString();
                //"LMSYSTEM", "LMLAYOUTID"
                string expression = "LMSYSTEM='" + sys + "' and LMLAYOUTID='" + lay + "'";
                DataRow[] rows = LocalData.LAYOUTMASTER.Select(expression);
                setUI.LAYOUTMASTER.Clear();
                setUI.Merge(rows);
                vwLAYOUTMASTER.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

        private void cmdLoadLeaf_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RefreshLeaf();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
      
        private void cmdNewLeaf_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //"PRSYSTEM", "PRLEDID", "PRPROGRAMID"
                //"LFSYSTEM", "LFLEDID", "LFPROGRAMID", "LFLEAFID",
                string LEAFID = m_led.Right("00" + dwPROGRAM["PRMAXLEFID"].ToString(), 2) + "0";
                DataRow dwTemp = LocalData.LEAF.NewLEAFRow();
                dwTemp["PRSYSTEM"] = dwPROGRAM["PRSYSTEM"];
                dwTemp["LFLEDID"] = dwPROGRAM["PRLEDID"];
                dwTemp["LFPROGRAMID"] = dwPROGRAM["PRPROGRAMID"];
                dwTemp["LFLEAFID"] = LEAFID;
                dwPROGRAM["PRMAXLEFID"] = System.Convert.ToInt32(dwPROGRAM["PRMAXLEFID"].ToString()) + 1;
                MergeData(dwPROGRAM);
                MergeData(dwTemp);
                RefreshLeaf();
                while (true)
                {
                    if (dwLEAF == null)
                        break;
                    if (dwLEAF["LFLEAFID"].ToString() != LEAFID)
                    {
                        vwLEAF.View.MoveCurrentToNext();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void cmdSaveLeaf_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MergeData(dwLEAF);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        
        private void cmdLeafArea_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //("LMSYSTEM", "LMLAYOUTID", "LMZONEID", "LMZONENAME", "LMLTOPX", "LMLTOPY", "LMLBOTTOMX", "LMLBOTTOMY", "LMRTOPX", "LMRTOPY", "LMRBOTTOMX", "LMRBOTTOMY"
                //LYXPOINT
                //LYYPOINT
                //"LFSYSTEM", "LFLEDID", "LFPROGRAMID" LFLEAFID
                //"LDSYSTEM", "LDLEDID", "LDPROGRAMID", "LDLEAFID",LDAREAID,
                RefreshLayOutMaster();
                string zoneid = "";
                decimal LYXPOINT = System.Convert.ToDecimal(dwLAYOUT["LYXPOINT"].ToString());
                decimal LYYPOINT = System.Convert.ToDecimal(dwLAYOUT["LYYPOINT"].ToString());
                DataRow dwLeafRow = LocalData.LEAFDETAIL.NewLEAFDETAILRow();
                decimal value = 0,src=0;
                string sys = dwLEAF["LFSYSTEM"].ToString();
                string led = dwLEAF["LFLEDID"].ToString();
                string pgid = dwLEAF["LFPROGRAMID"].ToString();
                string lfid = dwLEAF["LFLEAFID"].ToString();
                string area = "";
                DataRow[] rows = null;
                string expression = "";
                foreach (DataRow dwTemp in setUI.LAYOUTMASTER.Rows)
                { 
                    zoneid = dwTemp["LMZONEID"].ToString();
                    area = zoneid;// m_led.Right("00" + zoneid, 2) + "0";
                    expression = "LDSYSTEM='" + sys + "' and LDLEDID='" + led + "' and LDPROGRAMID='" + pgid + "' and LDLEAFID='" + lfid + "' and LDAREAID='" + area + "'";
                    rows = LocalData.LEAFDETAIL.Select(expression);
                    dwLeafRow = null;
                    foreach (DataRow dws in rows)
                    {
                        dwLeafRow = dws;
                    }
                    if (dwLeafRow == null)
                    {
                        dwLeafRow = LocalData.LEAFDETAIL.NewLEAFDETAILRow();
                        dwLeafRow["LDSYSTEM"] = sys;
                        dwLeafRow["LDLEDID"] = led;
                        dwLeafRow["LDPROGRAMID"] = pgid;
                        dwLeafRow["LDLEAFID"] = lfid;
                        dwLeafRow["LDAREAID"] = area;
                    }
                    #region======LTOPX========
                    src = System.Convert.ToDecimal(dwTemp["LMLTOPX"].ToString());
                    value =xPoint*(src/LYXPOINT);
                    if (value > xPoint)
                    {
                        value = xPoint;
                    }
                    dwLeafRow["LDLTOPX"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======RTOPX========
                    src = System.Convert.ToDecimal(dwTemp["LMRTOPX"].ToString());
                    value = xPoint * (src / LYXPOINT);
                    if (value > xPoint)
                    {
                        value = xPoint;
                    }
                    dwLeafRow["LDRTOPX"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======LBX========
                    src = System.Convert.ToDecimal(dwTemp["LMLBOTTOMX"].ToString());
                    value = xPoint * (src / LYXPOINT);
                    if (value > xPoint)
                    {
                        value = xPoint;
                    }
                    dwLeafRow["LDLBOTTOMX"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======RBX========
                    src = System.Convert.ToDecimal(dwTemp["LMRBOTTOMX"].ToString());
                    value = xPoint * (src / LYXPOINT);
                    if (value > xPoint)
                    {
                        value = xPoint;
                    }
                    dwLeafRow["LDRBOTTOMX"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======LTOPY========
                    src = System.Convert.ToDecimal(dwTemp["LMLTOPY"].ToString());
                    value = yPoint * (src / LYYPOINT);
                    if (value > yPoint)
                    {
                        value = yPoint;
                    }
                    dwLeafRow["LDLTOPY"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======RTOPY========
                    src = System.Convert.ToDecimal(dwTemp["LMRTOPY"].ToString());
                    value = yPoint * (src / LYYPOINT);
                    if (value > yPoint)
                    {
                        value = yPoint;
                    }
                    dwLeafRow["LDRTOPY"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======LBY========
                    src = System.Convert.ToDecimal(dwTemp["LMLBOTTOMY"].ToString());
                    value = xPoint * (src / LYYPOINT);
                    if (value > yPoint)
                    {
                        value = yPoint;
                    }
                    dwLeafRow["LDLBOTTOMY"] = System.Math.Round(value, 0).ToString();
                    #endregion
                    #region======RBY========
                    src = System.Convert.ToDecimal(dwTemp["LMRBOTTOMY"].ToString());
                    value = yPoint * (src / LYYPOINT);
                    if (value > yPoint)
                    {
                        value = yPoint;
                    }
                    dwLeafRow["LDRBOTTOMY"] = System.Math.Round(value, 0).ToString();
                    #endregion 
                    MergeData(dwLeafRow);
                }
                expression = "LDSYSTEM='" + sys + "' and LDLEDID='" + led + "' and LDPROGRAMID='" + pgid + "' and LDLEAFID='" + lfid + "' and LDAREAID<='" + area + "'";
                DataTable tb= LocalData.LEAFDETAIL.Select(expression).CopyToDataTable();
                tb.TableName = "LEAFDETAIL";
                LocalData.LEAFDETAIL.Clear();
                LocalData.Merge(tb);
                RefreshLeafDetail();
            }
            catch (Exception ex)
            { 
            }
        }

        private void lDCONTENTTYPEComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string conty = dwCONTENTTYPE["CTCONTENTTYPE"].ToString();
            switch (conty)
            { 
                  /*  CCOUNT
CMOVIE
CPICTURE
STEXT*/
                case "STEXT":
                    tabText.Focusable = true;
                    break;
                default:
                    break;
            }

        }

        private void lFLEAFIDComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                RefreshLeafDetail();
            }
            catch (Exception ex)
            { }

        }

        private void lDAREAIDComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
